package com.huxin.servelt;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.BooksInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/user/topBooks")
public class UsertopBooksServlet extends HttpServlet {
    private Userservice userservice=new Userservice();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取请求方法
        req.setCharacterEncoding("utf-8");
        //处理响应乱码
        resp.setContentType("text/json;charset=utf-8");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        // 允许指定的请求方法访问
        resp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        // 允许自定义的请求头部
        resp.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");

        List<BooksInfo> booksInfoList=userservice.topBooks();
        String jsonString = JSON.toJSONString(booksInfoList);
        System.out.println(jsonString);
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
